/*
 * Hidden Equity LLC
 * Author: Maksym Fedyay, Kyiv, mcsimf@gmail.com
 * Date: 29.02.2012
 */
package com.hiddenequity.acts;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import com.hiddenequity.campaign_service.CampaignResponseParser;
import com.hiddenequity.campaign_service.CampaignService;
import com.hiddenequity.report_definition_service.PredicateOperator;
import com.hiddenequity.report_definition_service.ReportDefinitionResponseParser;
import com.hiddenequity.report_definition_service.ReportDefinitionService;
import com.hiddenequity.report_definition_service.ReportDialog;
import com.hiddenequity.report_definition_service.ReportField;
import com.hiddenequity.report_definition_service.ReportType;
import com.hiddenequity.utils.AppPreferences;
import com.hiddenequity.utils.AuthService;
import com.hiddenequity.utils.Communication;
import com.hiddenequity.utils.DateProcessor;
import com.hiddenequity.utils.UrlBuncher;

import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;

public class Act_CampaignList extends Act_Super {

	public static ArrayList<HashMap<String, String>> campaignsList = new ArrayList<HashMap<String, String>>();

	private int dateRangeItem;
	private int statusItem;
		
	Spinner spinnerDateRange;
	Spinner spinnerStatus;
	
	
	@Override
	public void onConfigurationChanged(Configuration newConfig) {
		super.onConfigurationChanged(newConfig);
	}


	
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		
		super.onCreate(savedInstanceState);
		setContentView(R.layout.act_campaign_list);

		title.setText(R.string.title_campaigns_list);
		sub_title.setVisibility(View.GONE);
		
		spinnerDateRange = (Spinner) findViewById(R.id.spin_CampaignDateRange);
		dateRangeItem = AppPreferences.LoadPreferenceInt("dateRange", 0,
				getApplication());
		inflateSpinner(spinnerDateRange, R.array.arr_dateRanges, dateRangeItem);

		spinnerDateRange
				.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
					public void onItemSelected(AdapterView<?> parent,
							View view, int pos, long id) {
						try {
							if (pos != dateRangeItem) {
								AppPreferences.SavePreferenceInt("dateRange",
										pos, getApplication());
								dateRangeItem = pos;
								runOperationAsync(OPERATION_GET_LIST, getString(R.string.progress_loading), false);
							}
						} catch (Exception e) {
							e.printStackTrace();
						}

					}

					public void onNothingSelected(AdapterView<?> parent) {
					}

				});

		// Populate status list
		spinnerStatus = (Spinner) findViewById(R.id.spin_CampaignStatus);
		statusItem = AppPreferences.LoadPreferenceInt("status", 0, getApplication());
		inflateSpinner(spinnerStatus, R.array.arr_status, statusItem);

		spinnerStatus
				.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
					public void onItemSelected(AdapterView<?> parent,
							View view, int pos, long id) {
						try {
							if (pos != statusItem) {
								AppPreferences.SavePreferenceInt("status", pos,
										getApplication());
								statusItem = pos;
								runOperationAsync(OPERATION_GET_LIST, getString(R.string.progress_loading), false);
							}
						} catch (Exception e) {
							e.printStackTrace();
						}

					}

					public void onNothingSelected(AdapterView<?> parent) {
					}

				});

		
		//Set adapter and context menu for listview
		final ListView lv = (ListView) findViewById(R.id.list_Campaign);
		lv.setAdapter(campaignListAdapter);
		registerForContextMenu(lv);

		lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
			public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
				Map<String, String> itemMap = (Map<String, String>) lv.getItemAtPosition(position);
				if(!itemMap.get("status").equals("DELETED")){
					Intent intent = new Intent(getBaseContext(), Act_AdGroupList.class);
					intent.putExtra("campaignId", itemMap.get("id"));
					intent.putExtra("biddingStrategy", itemMap.get("biddingStrategy"));
					intent.putExtra("campaignName",	itemMap.get("name"));
					intent.putExtra("campaignBudget",	itemMap.get("budget"));
					startActivityForResult(intent, REQUEST_NO_REFRESH_REQUIRED);					
				}
			}
		});

		//Filter toggler
		filterToggler(AppPreferences.LoadPreferenceInt("filterToggle", 0, getApplication()));
		final Button btn_toggleFilter = (Button) findViewById(R.id.btn_toggleFilter);
		btn_toggleFilter.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				LinearLayout filter = (LinearLayout) findViewById(R.id.ll_filter);
				filterToggler(filter.getVisibility());
			}
		});

		if(AppPreferences.LoadPreferenceInt("accountNumber", -1, getApplication()) == -1){
			Intent intent = new Intent(Act_CampaignList.this.getApplicationContext(),
					Act_Login.class);
			startActivityForResult(intent, REQUEST_REFRESH_REQUIRED);
			return;
		}
			
		runOperationAsync(OPERATION_GET_LIST, getString(R.string.progress_loading), false);
	}// onCreate
	
	
	
	/*
	 * Handles requested activity result
	 */
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		
    	filterToggler(AppPreferences.LoadPreferenceInt("filterToggle", 0, getApplication()));

    	if(resultCode == RESPONSE_FINISH_REQUIRED){
			finishActivityWithResult(RESPONSE_FINISH_REQUIRED);
		} else if(resultCode == RESPONSE_GOTO_MAIN_ACTIVITY){
			runOperationAsync(OPERATION_GET_LIST, getString(R.string.progress_loading), false);
		} else if(isFilterChanged(statusItem, dateRangeItem)){
			statusItem = AppPreferences.LoadPreferenceInt("status", 0, getApplication());
			dateRangeItem = AppPreferences.LoadPreferenceInt("dateRange", 0, getApplication());
			runOperationAsync(OPERATION_GET_LIST, getString(R.string.progress_refreshing), false);
		} else if(requestCode == REQUEST_REFRESH_REQUIRED && resultCode == RESPONSE_REFRESH_REQUIRED){
			runOperationAsync(OPERATION_GET_LIST, getString(R.string.progress_refreshing), false); 			
		}
    	
		spinnerStatus.setSelection(statusItem);
		spinnerDateRange.setSelection(dateRangeItem);
    	
	}// onActivityResult

	
	
	/*
	 * Inflate Option menu called when menu button pressed (non-Javadoc)
	 * @see android.app.Activity#onCreateOptionsMenu(android.view.Menu)
	 */
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.option_campaign_list, menu);
		return true;
	}// onCreateOptionsMenu

	
	
	/*
	 * Option menu selection handler (non-Javadoc)
	 * @see android.app.Activity#onOptionsItemSelected(android.view.MenuItem)
	 */
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		Intent intent;

		switch (item.getItemId()) {
			case R.id.refresh_Campaign: 
				runOperationAsync(OPERATION_GET_LIST, getString(R.string.progress_refreshing), false);
				break;
	            
			case R.id.add_Campaign:
				intent = new Intent(this.getApplicationContext(),
						Act_CampaignGeneral.class);
				startActivityForResult(intent, REQUEST_REFRESH_REQUIRED);
				break;
	
			case R.id.switch_AccountCampaign:
				AuthService.performLogout(getApplication());
				intent = new Intent(this.getApplicationContext(), Act_Login.class);
				startActivityForResult(intent, REQUEST_REFRESH_REQUIRED);
				break;
				
			case R.id.option_reports:
				showRedirectReportDialog();
				break;
				
			case R.id.option_extensions:
				showRedirectExtensionDialog();
				break;
		}
		
		return super.onOptionsItemSelected(item);
	}// onOptionsItemSelected


	
	/*
     * Show dialog with for user redirection
     */
    private void showRedirectReportDialog(){
    	
    	final CharSequence[] items = {
    			getString(R.string.dlg_account_stats), 
    			getString(R.string.dlg_conversions),
    			getString(R.string.dlg_account_geo_performance),
    			getString(R.string.dlg_account_query_performance)
    		};

    	AlertDialog.Builder builder = new AlertDialog.Builder(this);
    	builder.setTitle(R.string.title_reports);
    	builder.setItems(items, new DialogInterface.OnClickListener() {
    	    public void onClick(DialogInterface dialog, int item) {
    	        // Account report
    	    	if(items[item].equals(getString(R.string.dlg_account_stats))){
    	    		String[] fields = new String[] {
    	    				ReportField.ACCOUNT_DESCRIPTIVE_NAME,
    	    				ReportField.CLICKS,
        	    			ReportField.COST,
        	    			ReportField.IMPRESSION,
        	    			ReportField.TOTAL_CONV_VALUE
        	    		};
    	    		
    	    		String reportDefinition = ReportDefinitionService.defineReport(
    	    				ReportType.ACCOUNT_PERFORMANCE_REPORT, fields, dateRangeItem, null);
    	    		
    	    		
    	    		String report = Communication.requestAdHogRequest(UrlBuncher.URL_ADHOC_REPORT, reportDefinition);
    	    		ReportDefinitionResponseParser parser = new ReportDefinitionResponseParser(report);
    	    		ArrayList<Map<String, String>> columns = parser.getReportColumns();
    	    		ArrayList<Map<String, String>> rows = parser.getReportRows(columns);
    	    		ReportDialog.showReport(Act_CampaignList.this, rows, columns, 0, R.string.msg_title_account_report).show();
    	    		
    	    	}
    	    	
    	    	// Conversions
    	    	if(items[item].equals(getString(R.string.dlg_conversions))){
    	            Intent intent = new Intent(getBaseContext(), Act_ConversionList.class);
    	            startActivityForResult(intent, REQUEST_NO_REFRESH_REQUIRED);
    	    	}
    	    	
    	    	// GEO report
    	    	if(items[item].equals(getString(R.string.dlg_account_geo_performance))){
    	            Intent intent = new Intent(getBaseContext(), Act_GeoReport.class);
    	            intent.putExtra("dateRangeItem", dateRangeItem);
    	            startActivityForResult(intent, REQUEST_NO_REFRESH_REQUIRED);
    	    	}  
    	    	
    	    	// Query report
    	    	if(items[item].equals(getString(R.string.dlg_account_query_performance))){
    	    		String[] fields = new String[] {
    	    				ReportField.QUERY,
    	    				ReportField.MATCH_TYPE,
        	    			ReportField.CLICKS,
        	    			ReportField.COST,
        	    			ReportField.IMPRESSION
        	    		};
    	    		
    	    		String reportDefinition = ReportDefinitionService.defineReport(
    	    				ReportType.SEARCH_QUERY_PERFORMANCE_REPORT, fields, dateRangeItem, null);

    	    		String report = Communication.requestAdHogRequest(UrlBuncher.URL_ADHOC_REPORT, reportDefinition);
    	    		ReportDefinitionResponseParser parser = new ReportDefinitionResponseParser(report);
    	    		ArrayList<Map<String, String>> columns = parser.getReportColumns();
    	    		ArrayList<Map<String, String>> rows = parser.getReportRows(columns);
    	    		ReportDialog.showReport(Act_CampaignList.this, rows, columns, 0, R.string.msg_title_search_report).show();
    	    	}
    	    }
    	});
    	AlertDialog alert = builder.create();
        alert.show();

    }//showRedirectDialog
	
    
	
	/*
     * Show dialog with for user redirection
     */
    private void showRedirectExtensionDialog(){
    	
    	final CharSequence[] items = {
    			getString(R.string.dlg_locationextension), 
    			getString(R.string.dlg_sitelinkextension)
    		};

    	AlertDialog.Builder builder = new AlertDialog.Builder(this);
    	builder.setTitle(R.string.title_ad_extension);
    	builder.setItems(items, new DialogInterface.OnClickListener() {
    	    public void onClick(DialogInterface dialog, int item) {
    	        
    	    	if(items[item].equals(getString(R.string.dlg_sitelinkextension))){
    	            Intent intent = new Intent(getBaseContext(), Act_SiteLinkExtensionList.class);
    	            startActivityForResult(intent, REQUEST_NO_REFRESH_REQUIRED);
    	    	}

    	    	if(items[item].equals(getString(R.string.dlg_locationextension))){
    	            Intent intent = new Intent(getBaseContext(), Act_LocationExtensionList.class);
    	            startActivityForResult(intent, REQUEST_NO_REFRESH_REQUIRED);
    	    	}   	    	
    	    }
    	});
    	AlertDialog alert = builder.create();
        alert.show();
    }//showRedirectDialog
    
    
    
	/*
	 * Inflate Context menu when long press occurred (non-Javadoc)
	 * @see android.app.Activity#onCreateContextMenu(android.view.ContextMenu,
	 * android.view.View, android.view.ContextMenu.ContextMenuInfo)
	 */
	String selectedCampaignId;
	String selectedCampaignName;
	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenuInfo menuInfo) {
		super.onCreateContextMenu(menu, v, menuInfo);
		
		switch (v.getId()) {

		case R.id.list_Campaign:
			AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo;
			ListView list = (ListView) v;
			Map<String, String> itemMap = (Map<String, String>) list
					.getItemAtPosition(info.position);

			selectedCampaignId = itemMap.get("id");
			selectedCampaignName = itemMap.get("name");
			
			String status = itemMap.get("status");

			menu.add(0, 1, 0, R.string.campaign_general);
			menu.add(0, 2, 0, R.string.campaign_geo);
			menu.add(0, 3, 0, R.string.campaign_device);
			menu.add(0, 4, 0, R.string.campaign_lang);
			menu.add(0, 5, 0, R.string.performance_report);
			
			if (!status.equals("ACTIVE"))
				menu.add(0, 6, 0, R.string.status_active);

			if (!status.equals("PAUSED"))
				menu.add(0, 7, 0, R.string.status_pause);

			if (!status.equals("DELETED"))
				menu.add(0, 8, 0, R.string.status_delete);

			break;
		}

		menu.setHeaderTitle(getString(R.string.tit_context_campaign));
	}// onCreateContextMenu

	
	
	/*
	 * Context menu selection handler (non-Javadoc)
	 * @see android.app.Activity#onOptionsItemSelected(android.view.MenuItem)
	 */
	@Override
	public boolean onContextItemSelected(MenuItem item) {
		Intent intent;

		switch (item.getItemId()) {

		case 1:
			intent = new Intent(this.getApplicationContext(),
					Act_CampaignGeneral.class);
			intent.putExtra("campaignId", selectedCampaignId);
			startActivityForResult(intent, REQUEST_REFRESH_REQUIRED);
			break;

		case 2:
			intent = new Intent(this.getApplicationContext(),
					Act_CampaignGeoTarget.class);
			intent.putExtra("campaignId", selectedCampaignId);
			intent.putExtra("campaignName", selectedCampaignName);
			startActivityForResult(intent, REQUEST_NO_REFRESH_REQUIRED);
			break;

		case 3:
			intent = new Intent(this.getApplicationContext(),
					Act_CampaignDeviceTarget.class);
			intent.putExtra("campaignId", selectedCampaignId);
			intent.putExtra("campaignName", selectedCampaignName);
			startActivityForResult(intent, REQUEST_NO_REFRESH_REQUIRED);
			break;

		case 4:
			intent = new Intent(this.getApplicationContext(),
					Act_CampaignLangTarget.class);
			intent.putExtra("campaignId", selectedCampaignId);
			intent.putExtra("campaignName", selectedCampaignName);
			startActivityForResult(intent, REQUEST_NO_REFRESH_REQUIRED);
			break;
		
		case 5:
    		String[] fields = new String[] {
				ReportField.CAMPAIGN_NAME,
				ReportField.CLICKS,
				ReportField.COST,
				ReportField.IMPRESSION,
				ReportField.STATUS,
				ReportField.CONVERSION_VALUE,
				ReportField.AD_NETWORK_TYPE2,
				ReportField.DEVICE,
				ReportField.CLICK_TYPE
    		};
    		
    		String[] predicateValue = new String[]{
    				selectedCampaignId
			};
			
			String[] predicates = new String[]{
					ReportDefinitionService.setPredicator(ReportField.ID, 
							PredicateOperator.EQUALS, predicateValue)
			};

			String reportDefinition = ReportDefinitionService.defineReport(
    				ReportType.CAMPAIGN_PERFORMANCE_REPORT, fields, dateRangeItem, predicates);
    		String report = Communication.requestAdHogRequest(UrlBuncher.URL_ADHOC_REPORT, reportDefinition);
    		ReportDefinitionResponseParser parser = new ReportDefinitionResponseParser(report);
			ArrayList<Map<String, String>> columns = parser.getReportColumns();
    		ArrayList<Map<String, String>> rows = parser.getReportRows(columns);
			ReportDialog.showReport(Act_CampaignList.this, rows, columns, 4, R.string.msg_title_campaign_report).show();
			break;
		
		case 6:
			statusSetTo = STATUS_ACTIVE;
			runOperationAsync(OPERATION_SET_STATUS, getString(R.string.progress_applying), false);
			break;

		case 7:
			statusSetTo = STATUS_PAUSED;
			runOperationAsync(OPERATION_SET_STATUS, getString(R.string.progress_applying), false);
			break;

		case 8:
			statusSetTo = STATUS_DELETED;
			
			new AlertDialog.Builder(this)
					.setTitle(R.string.msg_title_conf_delete)		
					.setMessage(getString(R.string.msg_conf_campaign_delete) + " " + quote + selectedCampaignName + quote +  "?")
	    			.setPositiveButton(R.string.btn_Yes, deletionConfirmationListener)
		    		.setNegativeButton(R.string.btn_No, deletionConfirmationListener)
		    		.setCancelable(false)
		    		.create()
		    		.show();
			
			break;
		}
		return true;
	}//onContextItemSelected

	
	
	/*
	 * Dialog onClick Listener
	 */
	private DialogInterface.OnClickListener deletionConfirmationListener = new DialogInterface.OnClickListener() {
		public void onClick(DialogInterface dialog, int which) {

			switch (which){
	        
		        case DialogInterface.BUTTON_POSITIVE:
		        	runOperationAsync(OPERATION_SET_STATUS, getString(R.string.progress_applying), false);
		            break;
	
		        case DialogInterface.BUTTON_NEGATIVE:
		        	//Do nothing on cancel
		            break;
	        }
	        
		}
	};//DialogClickListener
	
	
	
	/*
	 * Change campaign status
	 */
	private void setCampaignStatus(){
		String response = Communication.postSOAP(
				CampaignService.URL_CAMPAIGN_SERVICE,
				CampaignService.setCampaignStatus(selectedCampaignId, statusSetTo));

		CampaignResponseParser campaignParser = new CampaignResponseParser(response);
		
		responseErrorString = campaignParser.checkForErrors();		
	}//setCampaignStatus
	
	
	
	/*
     * Base adapter for listview
     */
	private BaseAdapter campaignListAdapter = new BaseAdapter() {

		public int getCount() {
			int size = Act_CampaignList.campaignsList.size();
			return size;
		}

		public Object getItem(int position) {
			return Act_CampaignList.campaignsList.get(position);
		}

		public long getItemId(int position) {
			return position;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			View row = convertView;

			if (row == null) {
				LayoutInflater mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				row = mInflater.inflate(R.layout.list_row_campaign, null);
			}

			TextView campaignName = (TextView) row.findViewById(R.id.campaignName);
			TextView campaignId = (TextView) row.findViewById(R.id.campaignId);
			TextView campaignStatus = (TextView) row.findViewById(R.id.campaignStatus);
			TextView campaignBidingStrategy = (TextView) row.findViewById(R.id.campaignBidingStrategy);
			TextView campaign_clicks_value = (TextView) row.findViewById(R.id.campaign_clicks_value);
			TextView campaign_conv_value = (TextView) row.findViewById(R.id.campaign_conv_value);
			TextView campaign_cost_value = (TextView) row.findViewById(R.id.campaign_cost_value);
			TextView campaign_ctr_value = (TextView) row.findViewById(R.id.campaign_ctr_value);
			TextView campaignBudget = (TextView) row.findViewById(R.id.campaignBudget);
			ImageView img_campaign_status = (ImageView) row.findViewById(R.id.img_campaign_status);
			ImageView img_more =  (ImageView) row.findViewById(R.id.img_more);
			
			campaignName.setText(Act_CampaignList.campaignsList.get(position).get("name"));
			campaignId.setText(Act_CampaignList.campaignsList.get(position).get("id"));
			campaignBidingStrategy.setText(Act_CampaignList.campaignsList.get(position).get("biddingStrategy"));
			campaign_clicks_value.setText(Act_CampaignList.campaignsList.get(position).get("clicks"));
			campaign_conv_value.setText(Act_CampaignList.campaignsList.get(position).get("conv"));
			campaign_cost_value.setText(Act_CampaignList.campaignsList.get(position).get("cost"));
			campaign_ctr_value.setText(Act_CampaignList.campaignsList.get(position).get("ctr"));
			campaignBudget.setText(Act_CampaignList.campaignsList.get(position).get("budget"));
			
			
			String status = Act_CampaignList.campaignsList.get(position).get("status");
			campaignStatus.setText(status);
			
			if(status.equals("ACTIVE")){
				img_campaign_status.setImageResource(R.drawable.enable);
				img_more.setVisibility(View.VISIBLE);
			} else if(status.equals("PAUSED")){
				img_campaign_status.setImageResource(R.drawable.pause);
				img_more.setVisibility(View.VISIBLE);
			} else if(status.equals("DELETED")){
				img_campaign_status.setImageResource(R.drawable.delete);
				img_more.setVisibility(View.INVISIBLE);
			}

			return row;
		}
	};//campaignListAdapter

	

	/*
	 * Async operation
	 */
	private void runOperationAsync(final int operation, String message, boolean overlap) {

		//Stop if thread is alive
		if(thread.isAlive() && !overlap)
			return;
		
		//Show progress dialog
		final ProgressDialog progressDialog = ProgressDialog.show(this, "",
				message, true);

		//Check if Internet connection alive
		if(!isConnectionAvalable()){
			progressDialog.dismiss();
			return;
		}

		thread = new Thread(){
			public void run() {
				//Perform authentication
				new AuthService(Act_CampaignList.this, getApplication());
				//Perform operation
				if (AuthService.authToken != null) {
					try {
						switch (operation) {
							case OPERATION_GET_LIST:
								Act_CampaignList.this.obtainCampaignList();
								Act_CampaignList.this.getTip();
								break;
								
							case OPERATION_SET_STATUS:
								Act_CampaignList.this.setCampaignStatus();
								break;				
						}
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
				//Perform postoperation
				Act_CampaignList.this.runOnUiThread(new Runnable() {
					public void run() {
						
						progressDialog.dismiss();

						if (AuthService.authToken != null) {
							
							showOperationError(R.string.msg_title_error, responseErrorString);
							
							switch (operation) {
								case OPERATION_GET_LIST:
									campaignListAdapter.notifyDataSetChanged();
									showStatsTotal(SHOW_TOTALS_CLICKS_COST_CTR_CONV);
									showTip();
									break;
								
								case OPERATION_SET_STATUS:
									runOperationAsync(OPERATION_GET_LIST, getString(R.string.progress_refreshing), true);
									break;
							}
						} else {
							Intent intent = new Intent(Act_CampaignList.this.getApplicationContext(),
									Act_Login.class);
							startActivityForResult(intent, REQUEST_REFRESH_REQUIRED);
						}
					}
				});
			}
		};
		thread.start(); 		
			
	}//runOperationAsync

	
	
	/*
	 * getListOfCampaigns
	 */
	private void obtainCampaignList() {
		campaignsList.clear();

		CampaignResponseParser parser = new CampaignResponseParser(
				Communication.postSOAP(CampaignService.URL_CAMPAIGN_SERVICE,
						CampaignService.getCampaignList(
								DateProcessor.getDateRange(dateRangeItem),
								statusItem)));

		campaignsList.addAll(parser.getCampaignList());
		
		responseErrorString = parser.checkForErrors();
		
	}// getListOfCampaigns
	
}